/**
 * Looker  新闻整合系统
 * Author  :solosky
 * File    :AddModifier.java
 * Date    :May 14, 2009
 * Lisence : Apache License 2.0 
 */
package net.looker.monitor.modifier;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.logging.Logger;

import net.looker.data.DataManager;
import net.looker.data.Media;
import net.looker.data.News;
import net.looker.monitor.analyzer.NewsAnalyzer;
import net.looker.monitor.crawler.CrawlerException;
import net.looker.monitor.crawler.CrawlerGroup;
import net.looker.monitor.crawler.MediaCrawler;
import net.looker.monitor.crawler.WebCrawler;
import net.looker.monitor.disposer.Disposer;
import net.looker.monitor.disposer.MediaDisposer;
import net.looker.monitor.disposer.NewsDisposer;
import net.looker.monitor.parser.ImageParser;
import net.looker.monitor.parser.LinkParser;
import net.looker.monitor.parser.Parser;

/**新增更改器
 * @author solosky
 *
 */
public class AddModifier extends Modifier
{

	/**
	 * @param news
	 */
	public AddModifier(News news)
	{
		super(news);
	}


	/* (non-Javadoc)
	 * @see net.looker.work.Work#startWork()
	 */
	@Override
	public void startWork() 
	{
		Logger logger = Logger.getLogger("looker");
		logger.info("新增更改器开始运行:"+news.getAnchor());
		try{
			//先获取新闻内容
			crawlContent();
			//分析新闻中的附件
			parseMedia();
			//下载新闻中的附件
			crawlMedia();
			//处理新闻正文
			disposeNews();
			//保存新闻至数据库
			saveNews();
		}catch(IOException e){
			logger.warning("添加新闻出错："+e.getMessage());
		} catch (CrawlerException e) {
			logger.warning("添加新闻出错："+e.getMessage());
		} catch (InterruptedException e) {
			logger.warning("添加新闻出错："+e.getMessage());
		}
		logger.info("新增更改器完成:"+news.getAnchor());
	}
	
	/**
	 * 获取新闻正文信息
	 * @throws CrawlerException 
	 * @throws IOException 
	 */
	private void crawlContent() throws IOException, CrawlerException
	{
		//获取正文HTML
		WebCrawler crawler = new WebCrawler(news.getUrl(), news.getCategory().getSiteConfig().get("site.charset"));
		crawler.crawl();
		news.setSource((String) crawler.getCrawled());
		
		//分析新闻正文，提取正文作者等、、
		NewsAnalyzer analyzer = new NewsAnalyzer(news);
		analyzer.analyze();
		
	}
	
	/**
	 * 解析新闻中的附件
	 */
	private void parseMedia()
	{
		Parser imgParser  = new ImageParser(news);
		Parser linkParser = new LinkParser(news);  
		imgParser.parse();
		linkParser.parse();
	}
	
	/**
	 * 下载新闻中的附件
	 * @throws InterruptedException 
	 */
	public void crawlMedia() throws InterruptedException
	{
		CrawlerGroup crawlerGroup = new CrawlerGroup();
		Iterator<Media> it = news.getMediaList().iterator();
		Media media        = null;
		
		//添加附件下载器
		while (it.hasNext())
		{
			media = it.next();
			try {
				crawlerGroup.addCrawler(new MediaCrawler(media));
			} catch (MalformedURLException e) {
				// 如果出现错误的URL的话，标记这个文件错误
				media.MarkError();
				Logger.getLogger("looker").warning("新闻附件URL错误:"+media.getUrl());
			}
		}
		
		//启动下载组，并等待完成
		crawlerGroup.startAll();
		crawlerGroup.waitAllDone();
		
	}
	
	/**
	 * 保存新闻至数据库
	 */
	private boolean saveNews()
	{
		return DataManager.getInstance().add(news);
	}
	
	/**
	 * 处理新闻
	 */
	private void disposeNews()
	{
		//处理新闻
		Disposer disposer = new NewsDisposer(news);
		disposer.dispose();
	}

}
